package com.itheima.reggie.common;


public class UploadFile {/*
    @Value("${reggie.path}")
    private String path;

    *//**
     * 文件上传
     * @param file 上传的文件
     * @return 转存的文件名称
     *//*
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file)  {
        //file文件是一个临时文件,需要转存到指定位置,否则本次请求结束时临时文件会删除,
//        log.info(file.toString());
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        assert originalFilename != null;
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID重新洗生成文件名,防止文件名重复
        String fileName = UUID.randomUUID().toString()+suffix;
        //创建目录
        File dir = new File(path);
        if (!dir.exists()){
            //判断目录是否存在
            dir.mkdirs(); // 创建目录
        }
        //将文件转存到目录
        try {
            file.transferTo(new File(path+fileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //返回 转存的文件名
        return R.success(fileName);
    }

    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){

        //设置响应编码
        response.setContentType("image/jpeg");
        try {
            //读取文件
            FileInputStream fis = new FileInputStream(new File(path + name));
            //写文件
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            //关闭资源
            fis.close();
            outputStream.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

*/}
